/**
 * @description 对象判空（undefined && {}）
 */
const isEmptyObject=(object)=>{
    return !object || JSON.stringify(object) === '{}'?true:false
}

/**
 * @description 数组判空（undefined && []）
 */
const isArrayObject=(array)=>{
  return !array||array.length<0
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * @description 用计时器对输入的函数进行防抖处理
 * @param {Function} fn 
 * @param {number} [delay] 默认300 
 * @example 
 * searchValueChange: utils.debounce(function (e) {
 *   const searchBoxValue = e.detail.value
 *   this.setData({
 *     companyInfo: this.updateCompanyInfoDisable(this.data.companyInfo, searchList, searchBoxValue)
 *   })
 * }),
 */
const debounce=(fn,delay=300)=>{
  let timer=null
  return function(...args){
    if(timer)clearTimeout(timer)
    timer=setTimeout(()=>{fn.apply(this,args)},delay)
  }
}

/**
 * !未测试
 * @description 用计时器对输入的函数进行节流处理
 * @param {Function} fn 
 * @param {Int} delay 
 */
const throttle=(fn,delay=300)=>{
  let timer=null
  return function(...agrs){
    console.log(...agrs)
    if(!timer)timer=setTimeout(()=>{fn.apply(this.agrs)},delay)
  }
}


module.exports = {
  formatTime,
  debounce,
  isEmptyObject,
  isArrayObject
}
